package com.warmheart.core.util;

import java.util.regex.Pattern;

/**
 * 
 * @ClassName: RegexUtil
 * @Description: 正则验证工具类
 * @author JiaoYang
 * @date 2025-06-16 09:37:02
 *
 */
public class RegexUtil {

    /**
     * 
     * @Title: checkHanzi
     * @Description: 汉字验证
     * @author JiaoYang
     * @param text
     * @return
     */
    public static boolean checkHanzi(String text) {
        if (text == null || "".equals(text)) {
            return false;
        }
        String regex = "[\\u4E00-\\u9FA5]+";
        return Pattern.matches(regex, text);
    }

    /**
     * 
     * @Title: checkHanziWithLength
     * @Description: 带长度汉字验证
     * @author JiaoYang
     * @param text
     * @param minLength
     * @param maxLength
     * @return
     */
    public static boolean checkHanziWithLength(String text, int minLength, int maxLength) {
        if (text == null || "".equals(text)) {
            return false;
        }
        String regex = "[\\u4E00-\\u9FA5]{" + minLength + "," + maxLength + "}";
        return Pattern.matches(regex, text);
    }

    /**
     * 
     * @Title: checkMobile
     * @Description: 手机号码验证
     * @author JiaoYang
     * @param text
     * @return
     */
    public static boolean checkMobile(String text) {
        if (text == null || "".equals(text)) {
            return false;
        }
        String regex = "^(1[1-9]\\d{9}$)";
        return Pattern.matches(regex, text);
    }

    /**
     * 
     * @Title: checkPhone
     * @Description: 电话号码验证
     * @author JiaoYang
     * @param text
     * @return
     */
    public static boolean checkPhone(String text) {
        if (text == null || "".equals(text)) {
            return false;
        }
        String regex = "0\\d{2,3}[-]?\\d{7,8}|0\\d{2,3}\\s?\\d{7,8}|1[1-9]\\d{9}";
        return Pattern.matches(regex, text);
    }

    /**
     * 
     * @Title: checkEmail
     * @Description: 邮箱验证
     * @author JiaoYang
     * @param text
     * @return
     */
    public static boolean checkEmail(String text) {
        if (text == null || "".equals(text)) {
            return false;
        }
        String regex = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";
        return Pattern.matches(regex, text);
    }

    /**
     * 
     * @Title: checkEngAndNumber
     * @Description: 英数字验证
     * @author JiaoYang
     * @param text
     * @return
     */
    public static boolean checkEngAndNumber(String text) {
        if (text == null || "".equals(text)) {
            return false;
        }
        String regex = "^[0-9a-zA-Z]+$";
        return Pattern.matches(regex, text);
    }

    /**
     * 
     * @Title: checkEngAndNumber
     * @Description: 带长度英数字验证
     * @author JiaoYang
     * @param text
     * @return
     */
    public static boolean checkEngAndNumberWithLength(String text, int minLength, int maxLength) {
        if (text == null || "".equals(text)) {
            return false;
        }
        String regex = "^[0-9a-zA-Z]{" + minLength + "," + maxLength + "}$";
        return Pattern.matches(regex, text);
    }

    /**
     * 
     * @Title: checkEngAndNumUl
     * @Description: 英数字验证加下划线
     * @author JiaoYang
     * @param text
     * @return
     */
    public static boolean checkEngAndNumUl(String text) {
        if (text == null || "".equals(text)) {
            return false;
        }
        String regex = "^[0-9a-zA-Z_]+$";
        return Pattern.matches(regex, text);
    }

    /**
     * 
     * @Title: checkIllegal
     * @Description: 非法字符验证
     * @author JiaoYang
     * @param text
     * @return
     */
    public static boolean checkIllegal(String text) {
        if (text == null || "".equals(text)) {
            return false;
        }
        String regex = "[\\u4E00-\\u9FA5a-zA-Z0-9,.?!-_，。？！]+";
        return Pattern.matches(regex, text);
    }

    /**
     * 
     * @Title: checkFloat
     * @Description: 浮点数验证
     * @author JiaoYang
     * @param text
     * @return
     */
    public static boolean checkFloat(String text, int length1, int length2) {
        if (text == null || "".equals(text)) {
            return false;
        }
        String regex = "^\\d{1," + length1 + "}(\\.\\d{1," + length2 + "})?$";
        return Pattern.matches(regex, text);
    }

    /**
     * 
     * @Title: checkInteger
     * @Description: 正整数验证
     * @author JiaoYang
     * @param text
     * @return
     */
    public static boolean checkInteger(String text, int length) {
        if (text == null || "".equals(text)) {
            return false;
        }
        String regex = "^[1-9]\\d{0," + length + "}$";
        return Pattern.matches(regex, text);
    }

    /**
     * 
     * @Title: checkIdNumber
     * @Description: 身份证验证
     * @author JiaoYang
     * @param text
     * @return
     */
    public static boolean checkIdNumber(String text) {
        if (text == null || "".equals(text)) {
            return false;
        }
        // 定义判别用户身份证号的正则表达式（15位或者18位，最后一位可以为字母）
        String regularExpression = "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|"
                + "(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)";

        boolean matches = text.matches(regularExpression);

        // 判断第18位校验值
        if (matches) {
            if (text.length() == 18) {
                try {
                    char[] charArray = text.toCharArray();
                    // 前十七位加权因子
                    int[] idCardWi = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
                    // 这是除以11后，可能产生的11位余数对应的验证码
                    String[] idCardY = { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" };
                    int sum = 0;
                    for (int i = 0; i < idCardWi.length; i++) {
                        int current = Integer.parseInt(String.valueOf(charArray[i]));
                        int count = current * idCardWi[i];
                        sum += count;
                    }
                    char idCardLast = charArray[17];
                    int idCardMod = sum % 11;
                    if (idCardY[idCardMod].toUpperCase().equals(String.valueOf(idCardLast).toUpperCase())) {
                        return true;
                    } else {
                        return false;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
            }
        }
        return matches;
    }

}
